package com.sunday.volunteer.admin.ui.index;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.CacheUtils;
import com.sunday.common.utils.DeviceUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.member.base.BaseActivity;
import com.sunday.volunteer.admin.R;
import com.sunday.volunteer.admin.entity.Version;
import com.sunday.volunteer.admin.http.VolunteerClient;

import org.json.JSONObject;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/*
* 设置
* */
public class SettingActivity extends BaseActivity {

    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.tv_cache)
    TextView tvCache;
    @Bind(R.id.tv_version)
    TextView tvVersion;

    private int vision_code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        titleView.setText("设置");
        tvCache.setText(CacheUtils.getTotalCacheSize(mContext));
        String vision_name = DeviceUtils.getVersion(mContext);
        vision_code = DeviceUtils.getVersionCode(mContext);
        tvVersion.setText(vision_name);
    }

    @OnClick(R.id.clear_cache)
    public void onClick() {
        CacheUtils.clearAllCache(mContext);
        tvCache.setText(CacheUtils.getTotalCacheSize(mContext));
    }

    @OnClick(R.id.check_update)
    public void update() {
        checkUpdate();
    }

    private void checkUpdate(){
        showLoadingDialog(0);
        Call<ResultDO<Version>> call = VolunteerClient.getVolunteerAdapter().getApkInfo(2,vision_code);
        call.enqueue(new Callback<ResultDO<Version>>() {
            @Override
            public void onResponse(Call<ResultDO<Version>> call, Response<ResultDO<Version>> response) {
                dissMissDialog();
                if (response.body()==null || isFinish){
                    return;
                }
                if (response.body().getCode()==0){
//                    Version version = response.body().getResult();
//                    Intent intent = new Intent(mContext, UpdateDialogActivity.class);
//                    HashMap<String,Object> params = new HashMap<>();
//                    params.put("update","Yes");
//                    params.put("version",version.getVersionName());
//                    params.put("update_log",version.getFileLogs());
//                    params.put("version",version.getVersionCode());
//                    params.put("path",version.getDownLoadUrl());
//                    params.put("target_size",Long.parseLong(version.getFizeSize())/1024/1024+"M");
//                    params.put("new_md5",version.getFileMd5());
//                    UpdateResponse updateResponse = new UpdateResponse(new JSONObject(params));
//                    intent.putExtra("response",updateResponse);
//                    intent.putExtra("force",true);
//                    startActivity(intent);
                }else {
                    ToastUtils.showToast(mContext,response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Version>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext,"网络错误");
            }
        });
    }
}
